<?php
/*
Author: Sergio Caffoz 
Date 20141018
Profesor Entity
*/

 class DateUtils{
 	public static function getWeekFromMondayToSunday(){
 		$week = array();
 		 
 		$hoy = strtotime('now');
 		$monday = "";
 		if(date("N",$hoy) == "1"){
 			$monday = $hoy;
 		}else{
 			$monday = strtotime('last Monday',$hoy);
 		}
 		$tuesday = strtotime('+1 day',$monday);
 		$wednesday = strtotime('+2 day',$monday);
 		$thursday = strtotime('+3 day',$monday);
 		$friday = strtotime('+4 day',$monday);
 		$saturday = strtotime('+5 day',$monday);
 		$sunday = strtotime('+6 day',$monday);
 		 
 		$week[] = DateUtils::getDiaObject($monday);
 		$week[] = DateUtils::getDiaObject($tuesday);
 		$week[] = DateUtils::getDiaObject($wednesday);
 		$week[] = DateUtils::getDiaObject($thursday);
 		$week[] = DateUtils::getDiaObject($friday);
 		$week[] = DateUtils::getDiaObject($saturday);
 		$week[] = DateUtils::getDiaObject($sunday);
 		 
 		return $week;
 		 
 		 
 	}
 	
 	public static function getWeekNext7Days($weekNumber){
 		$week = array();
 	
 		$day1 = strtotime('now');
//  		if(date("N",$hoy) == "1"){
 		if($weekNumber!=0){
 			$day1  = strtotime(($weekNumber * 7).' day');
 		}
 		
 		$day2 = strtotime('+1 day',$day1);
 		$day3 = strtotime('+2 day',$day1);
 		$day4 = strtotime('+3 day',$day1);
 		$day5 = strtotime('+4 day',$day1);
 		$day6 = strtotime('+5 day',$day1);
 		$day7 = strtotime('+6 day',$day1);
 	
 		$week[] = DateUtils::getDiaObject($day1);
 		$week[] = DateUtils::getDiaObject($day2);
 		$week[] = DateUtils::getDiaObject($day3);
 		$week[] = DateUtils::getDiaObject($day4);
 		$week[] = DateUtils::getDiaObject($day5);
 		$week[] = DateUtils::getDiaObject($day6);
 		$week[] = DateUtils::getDiaObject($day7);
 	
 		return $week;
 	
 	
 	}
 	
 	public static function getFromToWeekNext7($weekNumber){

 		$from = strtotime('now');
 		$to = strtotime('+6 day');
 		
 		if($weekNumber!=0){
 			$from = strtotime(($weekNumber * 7).' day');
 			$to = strtotime((($weekNumber * 7)+6).' day');
 		}
 		
 		return array($from,$to);
 	}
 	
 	public static function getFromToWeek(){
 		
 	
 		$hoy = strtotime('now');
 		$monday = null;
 		if(date("N",$hoy) == "1"){
 			$monday = $hoy;
 		}else{
 			$monday = strtotime('last Monday',$hoy);
 		}
 		$sunday= $sunday = strtotime('+6 day',$monday);
 		
 		return array($monday,$sunday);
 	}
 	public static function getDiaObject($date){
 		$dias = array("","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado","Domingo");
 		$meses = array("","Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
 		$dia = new Dia();
 		$dia->setId(date("N",$date));
 		$dia->setNombre($dias[date("N",$date)]);
 		$dia->setNro(date("d",$date));
 		$dia->setMes($meses[date("n",$date)]);
 		$feriado = DAODia::isFeriado($date);
 		$dia->setDateInstance($date);
 		if($feriado=='N' && date("N",$date) == "7"){
 			$feriado='D';
 		}
 		$dia->setFeriado($feriado);
 		
 		return $dia;
 	}
 	
 	public static function getMysqlDate($date){ 			
 		return date("Y-m-d H:i:s",$date);
 	}
 	
 	public static function formatToLocalDate($date){
 		return date("d/m/Y",strtotime($date));
 	}
 	
 	public static function getNowMysqlDate(){
 		return date("Y-m-d H:i:s");
 	}
 	
 	public static function compareWithNow($date){
 		
 		$todays_date = date("Y-m-d");
 		
 		$today = strtotime($todays_date);
 		$date_toCompare = strtotime($date);
 		
 		if ($date_toCompare > $today) {
 			return "mayor";
 		} else {
 			return "menor";
 		}
 	}

 	public static function compareWithNow2($date,$hour){
 		$fecha = strtotime($hour.' hours', strtotime($date));
 		$now = strtotime("now");
 		if ($now > $fecha) {
 			return "mayor";
 		} else {
 			return "menor";
 		}
 		
 	}
 	
 	public static function getDateInAWeekFromHorario($idHorario){
		$horario = DAOHorario::getById($idHorario);
		$week = DateUtils::getWeekNext7Days(0);
		$date=null;
		$mySqlDate = null;
	 	foreach ($week as $dia) {
	      if($dia->id == $horario->dia){
	      	$date = $dia->dateInstance;
	      	$mySqlDate = date("Y-m-d",$date);
	      	$mySqlDate = $mySqlDate." ".$horario->hora.":00";
	      }
		}
 		return $mySqlDate;
 	}
 	
 }

?>
